package com.kollway.imageselector.util;

import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ParcelUtils {

    public static void writeToParcel(Parcel out, String obj) {
        if(obj != null) {
            out.writeInt(1);
            out.writeString(obj);
        } else {
            out.writeInt(0);
        }

    }

    public static void writeToParcel(Parcel out, Long obj) {
        if(obj != null) {
            out.writeInt(1);
            out.writeLong(obj.longValue());
        } else {
            out.writeInt(0);
        }

    }

    public static void writeToParcel(Parcel out, Integer obj) {
        if(obj != null) {
            out.writeInt(1);
            out.writeInt(obj.intValue());
        } else {
            out.writeInt(0);
        }

    }

    public static void writeToParcel(Parcel out, Float obj) {
        if(obj != null) {
            out.writeInt(1);
            out.writeFloat(obj.floatValue());
        } else {
            out.writeInt(0);
        }

    }

    public static void writeToParcel(Parcel out, Double obj) {
        if(obj != null) {
            out.writeInt(1);
            out.writeDouble(obj.doubleValue());
        } else {
            out.writeInt(0);
        }

    }

    public static void writeToParcel(Parcel out, Map obj) {
        if(obj != null) {
            out.writeInt(1);
            out.writeMap(obj);
        } else {
            out.writeInt(0);
        }

    }

    public static void writeToParcel(Parcel out, Date obj) {
        if(obj != null) {
            out.writeInt(1);
            out.writeLong(obj.getTime());
        } else {
            out.writeInt(0);
        }

    }

    public static String readFromParcel(Parcel in) {
        int flag = in.readInt();
        return flag == 1?in.readString():null;
    }

    public static <T extends Parcelable> void writeToParcel(Parcel out, T model) {
        if(model != null) {
            out.writeInt(1);
            out.writeParcelable(model, 0);
        } else {
            out.writeInt(0);
        }

    }

    public static <T extends List<?>> void writeToParcel(Parcel out, T model) {
        if(model != null) {
            out.writeInt(1);
            out.writeList(model);
        } else {
            out.writeInt(0);
        }

    }
}
